import { useRoute, useRouter } from 'vue-router';

import { useTabsRouterStore } from '@/store';

export const useTabHook = () => {
  const route = useRoute();
  const router = useRouter();
  const tabsRouterStore = useTabsRouterStore();

  /** 移除当前标签页 */
  const handleRemoveCurrentTab = () => {
    const value = route.path;
    const index = tabsRouterStore.tabRouters.findIndex((route) => route.path === value) as number;
    const options = { index, value };
    const nextRouter = tabsRouterStore.tabRouters[options.index + 1] || tabsRouterStore.tabRouters[options.index - 1];
    tabsRouterStore.subtractCurrentTabRouter({ path: options.value as string, routeIdx: options.index });
    if ((options.value as string) === route.path) router.push({ path: nextRouter.path, query: nextRouter.query });
  };
  return {
    handleRemoveCurrentTab,
  };
};
